package com.robaone.gwt.hierarcialdb.client.ui.menu;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.robaone.gwt.hierarcialdb.client.Hierarcialdbadmin;
import com.robaone.gwt.hierarcialdb.client.ListItem;
import com.robaone.gwt.hierarcialdb.client.ui.layout.AdminLayoutUi;

public class MenuState {
	private AdminLayoutUi parent;
	private MenuState(){
		this.parent = Hierarcialdbadmin.getLayout();
	}


	private void initialize() {
		final MenuUi menu = this.parent.getMenu();
		if(menu.isInitialized() == false){
			Hierarcialdbadmin.hdbService.getMenuItems(new AsyncCallback<String[]>(){

				@Override
				public void onFailure(Throwable caught) {
					caught.printStackTrace();
				}

				@Override
				public void onSuccess(String[] result) {
					try{
						menu.setItems(result);
					}catch(Exception e){
						onFailure(new Throwable(e));
					}
				}

			});
		}
	}


	public void setHome(){
		this.parent.getMenu().setActiveItem(0);
	}


	public static void go() {
		MenuState state = new MenuState();
		state.initialize();
		state.parent.getMenu().setActiveItem(0);
	}
}
